$(document).ready(function() {
    var dataSource = new kendo.data.DataSource({
        transport: {
            read: {
                url: appBosch.urlRead,
                dataType: "json"
            },
            parameterMap: function(options, operation) {
                var params = {};
                switch (operation)
                {
                    case 'read':
                        break;
                }
                return params;
            }
        },
        batch: true,
        schema: {
            model: {
                id: "id"
            }
        }
    });
    
    var dataSource2 = new kendo.data.DataSource({
        transport: {
            read: {
                url: appBosch.urlRead2,
                dataType: "json"
            },
            parameterMap: function(options, operation) {
                var params = {};
                switch (operation)
                {
                    case 'read':
                        break;
                }
                return params;
            }
        },
        batch: true,
        schema: {
            model: {
                id: "id"
            }
        }
    });

    $("#grid").kendoGrid({
        scrollable: true,
        filterable: true,
        selectable: true,
        navigatable: true,
        sortable: {
            mode: "single",
            allowUnsort: false
        },
        pageable: true,
        dataSource: dataSource,
        height: 520,
        columns: [
            {field: "testName", title: "Curso"},
            {field: "test_date", title: "Fecha"},
            {field: "location", title: "Lugar"},
            {command: [
                    {text: "Participar", click: register}
                ], title: "&nbsp;"
            }
        ]
    });
    
    $("#grid2").kendoGrid({
        scrollable: true,
        filterable: true,
        selectable: true,
        navigatable: true,
        sortable: {
            mode: "single",
            allowUnsort: false
        },
        pageable: true,
        dataSource: dataSource2,
        height: 520,
        columns: [
            {field: "testName", title: "Curso"},
            {field: "test_date", title: "Fecha"},
            {field: "location", title: "Lugar"},
            {command: [
                    {text: "Cancelar", click: unregister}
                ], title: "&nbsp;"
            }
        ]
    });
});

function register(e) {
    e.preventDefault();
    var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
    var id = dataItem.id;
    var url = appBosch.urlRegister + '/' + id;
    $.getJSON(url, {}, function(success) {
        if (success === true) {
            $("#grid").data('kendoGrid').dataSource.read();
            $("#grid2").data('kendoGrid').dataSource.read();
        }
    });
}

function unregister(e) {
    e.preventDefault();
    var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
    var id = dataItem.id;
    var url = appBosch.urlUnregister + '/' + id;
    $.getJSON(url, {}, function(success) {
        if (success === true) {
            $("#grid2").data('kendoGrid').dataSource.read();
            $("#grid").data('kendoGrid').dataSource.read();
        }
    });
}